package com.neusoft.movie.v_movies.adpater;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.neusoft.movie.v_movies.R;
import com.neusoft.movie.v_movies.activity.MoviesInfoActivity;
import com.neusoft.movie.v_movies.javabean.MoviesBean;
import com.squareup.picasso.Picasso;

import java.util.List;

import static com.neusoft.movie.v_movies.common.Constants.PHOTO_URL;

/**
 * Created by Administrator on 2017/12/26.
 */

public class MoviesAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{
    private Context mContext;
    private List<MoviesBean> mDataList;
    private LayoutInflater mLayoutInflater;
    public MoviesAdapter(Context mContext, List mDataList) {
        this.mContext = mContext;
        this.mDataList = mDataList;
        mLayoutInflater = LayoutInflater.from(mContext);
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View v = mLayoutInflater.from(viewGroup.getContext()).inflate(R.layout.fragment_movies, viewGroup, false);

        return new ViewHolder(v);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
     final MoviesBean entity = (MoviesBean)mDataList.get(position);
        if (null == entity)
            return;
        ViewHolder viewHolder = (ViewHolder) holder;
        viewHolder.name.setText(entity.getM_name());
        String pic = PHOTO_URL+entity.getM_pic();
        if (!pic.equals("")) {
            Picasso.with(mContext).load(pic).error(R.drawable.ic_movie_gray).into(viewHolder.image);
        }


        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext, MoviesInfoActivity.class);
                intent.putExtra("m_id",entity.getM_id());
                mContext.startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        return mDataList.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        TextView name;
        ImageView image;
        public ViewHolder(View view) {
            super(view);
            name = (TextView) view.findViewById(R.id.m_name);
            image = (ImageView) view.findViewById(R.id.m_pic);

        }
    }
}
